class Payment < ActiveRecord::Base
  belongs_to :user  
  validates_presence_of :amount
  validates_numericality_of :amount, :allow_blank => false
  before_destroy :rollback_saldo

  def validate
    if self.amount == 0
    errors.add(:amount, "nie może być zerem")
    end
  end

  after_save :change_saldo
  AMOUNTS = [['20gr',(0.2).to_f]]
  
  def change_saldo
     u = User.find(:first, :conditions => {:id => self.user_id} )
     u.saldo = u.saldo + self.amount
     u.save
  end

  def rollback_saldo
    u = User.find(:first, :conditions => {:id => self.user_id} )
    u.saldo = u.saldo - self.amount
    u.save
  end

end
